package nuist.hanniko.stu.webbackend.service.impls;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import nuist.hanniko.stu.webbackend.model.LwM2m.LwM2MResourceValue;
import nuist.hanniko.stu.webbackend.service.DeviceBriefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DeviceService {

    private final HistoryRecordServiceImpl historyRecordService;

    private final RedisRecordServiceImpl redisRecordService;

    private final DeviceBriefService deviceBriefService;

    @Autowired
    public DeviceService(HistoryRecordServiceImpl historyRecordService, RedisRecordServiceImpl redisRecordService, DeviceBriefService deviceBriefService) {
        this.historyRecordService = historyRecordService;
        this.redisRecordService = redisRecordService;
        this.deviceBriefService = deviceBriefService;
    }

    /*
     * Device Brief Region
     */
    public JSONArray getAllDevice() {
        return (JSONArray) JSON.toJSON(deviceBriefService.getAllDevice());
    }

    public JSONArray getDeviceByIndex(int start, int length) {
        return (JSONArray) JSON.toJSON(deviceBriefService.getDevicesByIndex(start, length));
    }

    /*
     * History Record Method Region
     */
    public JSONArray getHistoryRecordByDeviceId(String deviceId) {
        return historyRecordService.getDeviceRecord(deviceId);
    }

    public JSONArray getHistoryRecordByDeviceIdWithinPeriod(String deviceId,
                                                            Date begin,
                                                            Date end) {
        return historyRecordService.getDeviceRecordWithinPeriod(deviceId, begin, end);
    }

    public JSONArray getHistoryRecordByObjetId(String deviceId, String objectId) {
        return historyRecordService.getObjectRecord(deviceId, objectId);
    }

    public JSONArray getHistoryRecordByObjetIdWithinPeriod(String deviceId,
                                                           String objectId,
                                                           Date begin,
                                                           Date end) {
        return historyRecordService.getObjectRecordWithinPeriod(deviceId, objectId, begin, end);
    }

    public JSONArray getHistoryRecordByResourceId(String deviceId, String objectId, String resourceId) {
        return historyRecordService.getResourceRecord(deviceId, objectId, resourceId);
    }

    public JSONArray getHistoryRecordByResourceIdWithinPeriod(String deviceId,
                                                              String objectId,
                                                              String resourceId,
                                                              Date begin,
                                                              Date end) {
        return historyRecordService.getResourceRecordWithinPeriod(deviceId, objectId, resourceId, begin, end);
    }

    public int addRecord(LwM2MResourceValue value) {
        return historyRecordService.addRecord(value);
    }

    public int addRecord(List<LwM2MResourceValue> values) {
        return historyRecordService.addRecord(values);
    }

    /*
     * Real-time Record(redis) Method Region
     */
    public JSONArray getRealTimeResourceRecord(String deviceId, String objectId, String resourceId) {
        return redisRecordService.getResourceRecord(deviceId, objectId, resourceId);
    }

    public JSONArray getRealTimeObjectRecord(String deviceId, String objectId) {
        return redisRecordService.getObjectRecord(deviceId, objectId);
    }

    public JSONArray getRealTimeDeviceRecord(String deviceId) {
        return redisRecordService.getDeviceRecord(deviceId);
    }
}
